package cn.iocoder.yudao.adminserver.modules.system.controller.feedback;

import cn.iocoder.yudao.adminserver.modules.system.controller.feedback.vo.*;
import cn.iocoder.yudao.adminserver.modules.system.convert.feedback.SysFeedbackConvert;
import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.feedback.SysFeedbackDO;
import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.user.SysUserDO;
import cn.iocoder.yudao.adminserver.modules.system.service.feedback.SysFeedbackService;
import cn.iocoder.yudao.adminserver.modules.system.service.user.SysUserService;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "问题反馈")
@RestController
@RequestMapping("/system/feedback")
@Validated
public class SysFeedbackController {

    @Resource
    private SysFeedbackService feedbackService;

    @Resource
    private SysUserService userService;

    @PostMapping("/create")
    @ApiOperation("创建问题反馈")
    @PreAuthorize("@ss.hasPermission('system:feedback:create')")
    public CommonResult<Long> createFeedback(@Valid @RequestBody SysFeedbackCreateReqVO createReqVO) {
        return success(feedbackService.createFeedback(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新问题反馈")
    @PreAuthorize("@ss.hasPermission('system:feedback:update')")
    public CommonResult<Boolean> updateFeedback(@Valid @RequestBody SysFeedbackUpdateReqVO updateReqVO) {
        feedbackService.updateFeedback(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除问题反馈")
    @ApiImplicitParam(name = "id", value = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:feedback:delete')")
    public CommonResult<Boolean> deleteFeedback(@RequestParam("id") Long id) {
        feedbackService.deleteFeedback(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得问题反馈")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('system:feedback:query')")
    public CommonResult<SysFeedbackRespVO> getFeedback(@RequestParam("id") Long id) {
        SysFeedbackDO feedback = feedbackService.getFeedback(id);
        return success(SysFeedbackConvert.INSTANCE.convert(feedback));
    }

    @GetMapping("/list")
    @ApiOperation("获得问题反馈列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('system:feedback:query')")
    public CommonResult<List<SysFeedbackRespVO>> getFeedbackList(@RequestParam("ids") Collection<Long> ids) {
        List<SysFeedbackDO> list = feedbackService.getFeedbackList(ids);
        return success(SysFeedbackConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得问题反馈分页")
    @PreAuthorize("@ss.hasPermission('system:feedback:query')")
    public CommonResult<PageResult<SysFeedbackRespVO>> getFeedbackPage(@Valid SysFeedbackPageReqVO pageVO) {
        PageResult<SysFeedbackDO> pageResult = feedbackService.getFeedbackPage(pageVO);
        List<SysFeedbackRespVO> list = new ArrayList<>();
        pageResult.getList().forEach(obj -> {
            SysUserDO user = this.userService.getUser(Long.parseLong(obj.getCreator()));
            SysFeedbackRespVO respVO = SysFeedbackConvert.INSTANCE.convert(obj, user);
            list.add(respVO);
        });
        return success(new PageResult<SysFeedbackRespVO>(list, pageResult.getTotal()));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出问题反馈 Excel")
    @PreAuthorize("@ss.hasPermission('system:feedback:export')")
    @OperateLog(type = EXPORT)
    public void exportFeedbackExcel(@Valid SysFeedbackExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<SysFeedbackDO> list = feedbackService.getFeedbackList(exportReqVO);
        // 导出 Excel
        List<SysFeedbackExcelVO> datas = SysFeedbackConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "问题反馈.xls", "数据", SysFeedbackExcelVO.class, datas);
    }

}
